package com.topcheer.gateway.ribbon.db.predicate;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.PredicateKey;
import com.netflix.loadbalancer.Server;
import com.topcheer.gateway.ribbon.WeightServer;
import com.topcheer.gateway.ribbon.context.RibbonFilterContext;
import com.topcheer.gateway.ribbon.context.RibbonFilterContextHolder;
import com.topcheer.gateway.ribbon.db.rule.WeightRoundRule;

import java.util.List;

/**
 * @author xutao
 * @Package com.topcheer.gateway.ribbon.db.predicate
 * @Description: ${todo}
 * @date 2020/9/25 15:24
 */
public class WeightPredicate extends AbstractServerPredicate {

    WeightRoundRule weightRoundRule;

    public WeightPredicate(WeightRoundRule weightRoundRule) {
        this.weightRoundRule = weightRoundRule;
    }

    @Override
    public boolean apply(PredicateKey predicateKey) {
        Server server = predicateKey.getServer();
        final RibbonFilterContext context = RibbonFilterContextHolder.getCurrentContext();
        return true;
    }

    @Override
    public Optional<Server> chooseRoundRobinAfterFiltering(List<Server> servers, Object loadBalancerKey) {
        //        List<Server> eligible = getEligibleServers(servers, loadBalancerKey);
        final RibbonFilterContext context = RibbonFilterContextHolder.getCurrentContext();
        String path = context.get("path");
        List<WeightServer> serverList = Lists.newArrayList();// 从db查询
        if (serverList.size() == 0) {
            return Optional.absent();
        }
        return Optional.of(weightRoundRule.choose(serverList));
    }
}
